package vlms.controller;

import java.io.IOException;
import java.rmi.RemoteException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import vlms.service.VLMSServiceProxy;

import vlms.bean.*;
import wrapper.CartMovie;

/**
 * Servlet implementation class ViewCart
 */
public class ViewCart extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewCart() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		getUserCart(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		getUserCart(request,response);
	}

private void getUserCart(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException{
		
		String message = null; 
		String destination = "/home.jsp";
		HttpSession session = request.getSession();
		Person oPerson = (Person) session.getAttribute("Person");
		String sPersonId = String.valueOf(oPerson.getPersonid());
		
		try
		 {
			 VLMSServiceProxy proxy = new VLMSServiceProxy();
			 CartMovie[] aCartMovie = proxy.viewCart(sPersonId);
			 System.out.println("size: " + aCartMovie.length);
			 if(null == aCartMovie || aCartMovie.length < 0) {
				 
				 System.out.println("Entered");
				 
				 message = "Cart is empty";
				 request.setAttribute("message", message);
			 }	
			 request.setAttribute("CartMovie", aCartMovie);
			 session.setAttribute("CartMovie", aCartMovie);
			 destination = "/viewCart.jsp";
		 }
		 catch(RemoteException re)
		 {
			 destination = "/viewCart.jsp";
			 request.setAttribute("message", re.getMessage());
			 re.printStackTrace();
		 }
		
		
		
		RequestDispatcher rd = this.getServletContext().getRequestDispatcher(destination);
		rd.forward(request, response);
		
		
	}
}
